package com.djl.zenithbite.service.tag.impl;

import com.djl.zenithbite.common.response.BaseResponse;
import com.djl.zenithbite.dto.response.tag.LabelResponse;
import com.djl.zenithbite.entity.Tag;
import com.djl.zenithbite.entity.TagExample;
import com.djl.zenithbite.repository.TagMapper;
import com.djl.zenithbite.service.tag.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Override
    public BaseResponse<Map<String,List<LabelResponse>>> tagList() {
        List<Tag> tags = tagMapper.selectByExample(new TagExample());
        Map<String, List<Tag>> map = Optional.ofNullable(tags).orElse(new ArrayList<>()).stream().collect(Collectors.groupingBy(Tag::getTagType));

        Map<String,List<LabelResponse>> tagMap = new HashMap<>();
        for (Map.Entry<String, List<Tag>> entry : map.entrySet()){
            String key = entry.getKey();
            List<LabelResponse> list = entry.getValue().stream().map(tag -> {
                LabelResponse response = new LabelResponse();
                response.setKey(tag.getTagId());
                response.setValue(tag.getTagName());
                return response;
            }).collect(Collectors.toList());
            tagMap.put(key,list);
        }

        BaseResponse<Map<String,List<LabelResponse>>> response = new BaseResponse<>();
        response.setCode(BaseResponse.SUCCESS);
        response.setData(tagMap);

        return response;
    }
}
